package com.ghc.action;

import java.util.ArrayList;
import java.util.List;


import com.ghc.dao.MailDao;
import com.ghc.dao.ServiceDao;
import com.ghc.pojo.Service;
import com.ghc.service.MailServiceImpl;
import com.ghc.vo.MailVo;
import com.google.gson.Gson;

public class ServiceAction {
	
	private Service service = new Service();
	private String result;
	private ServiceDao serviceDao;
	private List<Service> serviceList = new ArrayList<Service>();
	private MailVo mailVo = new MailVo();
	private MailDao mailDao;
	public MailServiceImpl mailServiceImpl;
	
	public String addService(){
		try {
			serviceDao.addService(service);
			result = "submited";
		} catch (Exception e) {
			result = "fail";
		}
		return "success";
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String showServiceAllJS(){
		
		int allRow = serviceDao.getRow(service);
		serviceList = serviceDao.showServiceAll(service);
		List list = new ArrayList();
		list.add(allRow);
		list.add(serviceList);
		
		result = new Gson().toJson(list);
		return "success";
	}
	
	public String delService(){
		try {
			serviceDao.delService(service.getId());
			result = "del";
		} catch (Exception e) {
			result = "fail";
		}
		
		return "success";
	}
	public String sendSevMail(){
		try{
			mailServiceImpl.placeOrder(mailVo);
			mailDao.updateMailStatus(mailVo.getId());
		    serviceDao.changeSevStatus(service.getId());
	        result = "sended";
		} catch (Exception e) {
			e.printStackTrace();
        	result = "fail";
		}
		return "success";
	}
	
	public Service getService() {
		return service;
	}
	public void setService(Service service) {
		this.service = service;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public ServiceDao getServiceDao() {
		return serviceDao;
	}
	public void setServiceDao(ServiceDao serviceDao) {
		this.serviceDao = serviceDao;
	}


	public List<Service> getServiceList() {
		return serviceList;
	}

	public void setServiceList(List<Service> serviceList) {
		this.serviceList = serviceList;
	}

	public MailVo getMailVo() {
		return mailVo;
	}

	public void setMailVo(MailVo mailVo) {
		this.mailVo = mailVo;
	}

	public MailDao getMailDao() {
		return mailDao;
	}

	public void setMailDao(MailDao mailDao) {
		this.mailDao = mailDao;
	}

	public MailServiceImpl getMailServiceImpl() {
		return mailServiceImpl;
	}

	public void setMailServiceImpl(MailServiceImpl mailServiceImpl) {
		this.mailServiceImpl = mailServiceImpl;
	}
}
